package andy.demo.parseXML.sax.service;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import andy.demo.parseXML.sax.domain.Person;



/**  解析过程示例：
<?xml version="1.0" encoding="UTF-8"?>
<persons>
     <person id="1">
           <name>靓仔</name>
           <age>20</age>
     </person>
     <person id="2">
           <name>李开复</name>
           <age>40</age>
     </person>
</persons>

二：SAX的解析：
1，文件开始调用startDocument：这里可以做UTF-8编码的判断
2，<persons> 标签开始：调用startElement
3，从<persons>到<person>标签：调用characters 方法两次。第一次得到\n换行，第二次得到\t一个制表符位 
4，<person>标签，继续调用startElement，然后又两次调用characters方法。 
5，<name>标签，继续调用startElement
6，调用characters方法得到靓仔
7，调用endElement方法
……    

 *SAX的解析：
 * @author zhouhuan
 *
 */
public class XmlContentHandler extends DefaultHandler {
	
	private Person person;
	private ArrayList<Person> personList = new ArrayList<Person>();
	private String preTag;
	
	
	public ArrayList<Person>  getPersonList( ) {
		System.out.println("personList =" + personList);
		return personList;
	}
	
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		System.out.println("startDocument()-------------------------");
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
		System.out.println("endDocument()----------------------------");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		System.out.println("startElement() ：： uri="+uri +" localName =" + localName + " qName=" + qName + " attributes=" + attributes);
		if(localName != null) {
			if("person".equalsIgnoreCase(localName)) {
				person = new Person();
				
				person.setId(Integer.parseInt(attributes.getValue("id")));
				System.out.println("person================"  + person );
			}
			preTag = localName;
		}
		
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		System.out.println("endElement----------currentTag=" + localName);
		
		if("person".equals(localName) && person!=null){
			System.out.println("add person===========" + person);
			personList.add(person);
			person = null;
			System.out.println("personList" + personList);
		} else if("persons".equalsIgnoreCase(localName)) {
			preTag = null;
		}
	}

//	@Override
//	public void characters(char[] ch, int start, int length)
//			throws SAXException {
//		
////		String value = new String(ch);
//		String value = new String(ch, start, length);
//		System.out.println("value===" + value);
//		if("name".equalsIgnoreCase(preTag)) {
//			
//			System.out.println("set name");
//			person.setPersonName(value);
//			System.out.println("person set name------------" + person);
//		}else if("age".equalsIgnoreCase(preTag)){
//			
//			person.setAge(value);
//		}
//		
//	}
	
	
	/**
	 * 读取<name>弃天帝</name>中内容
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		System.out.println("***************currentTag=" + preTag);
		System.out.println("***************person=" + person);
		System.out.println("ch=" + ch.toString());
		String charstr = new String(ch);
		System.out.println("ch to String = " + charstr);
		System.out.println("start= " + start);
		System.out.println("length=" +length);
		
		if(person!=null){
			String data = new String(ch,start,length);
			if("name".equalsIgnoreCase(preTag))
			{
				System.out.println("person.getPersonName() =" + person.getPersonName());
				person.setPersonName(person.getPersonName() == null || person.getPersonName().equalsIgnoreCase("") ? data:person.getPersonName());
			}else if("age".equalsIgnoreCase(preTag))
			{
				person.setAge(person.getAge() == null  || "" .equalsIgnoreCase(person.getAge())? data: person.getAge());
			}
		}
	}
	
	
	
	
	

}
